package com.example.pt.mapper;

import com.example.pt.entity.UserFollow;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 用户关注Mapper接口
 */
@Mapper
public interface UserFollowMapper extends BaseMapper<UserFollow> {
    
    /**
     * 查询用户的关注列表
     * @param followerId 关注者ID
     * @return 关注列表
     */
    @Select("SELECT * FROM user_follow WHERE follower_id = #{followerId} AND status = 0 ORDER BY create_time DESC")
    List<UserFollow> selectFollowingsByFollowerId(@Param("followerId") Integer followerId);
    
    /**
     * 查询用户是否关注了指定用户
     * @param followerId 关注者ID
     * @param followedId 被关注者ID
     * @return 关注记录
     */
    @Select("SELECT * FROM user_follow WHERE follower_id = #{followerId} AND followed_id = #{followedId} LIMIT 1")
    UserFollow selectByFollowerIdAndFollowedId(@Param("followerId") Integer followerId, @Param("followedId") Integer followedId);
    
    /**
     * 取消关注
     * @param followerId 关注者ID
     * @param followedId 被关注者ID
     * @return 影响行数
     */
    @Update("UPDATE user_follow SET status = 1 WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    int unfollowUser(@Param("followerId") Integer followerId, @Param("followedId") Integer followedId);
    
    /**
     * 重新关注
     * @param followerId 关注者ID
     * @param followedId 被关注者ID
     * @return 影响行数
     */
    @Update("UPDATE user_follow SET status = 0, create_time = NOW() WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    int refollowUser(@Param("followerId") Integer followerId, @Param("followedId") Integer followedId);
} 